package com.jin.webvideo.externalInterfaces.video;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.base.util.CollectionsUtil;
import com.jin.webvideo.exception.UnavailableServiceException;
import com.jin.webvideo.externalInterfaces.PlatformSupport;
import com.jin.webvideo.externalInterfaces.entity.Filmography;
import com.jin.webvideo.externalInterfaces.entity.FilmographyInfo;
import com.jin.webvideo.util.sys.ClientUtil;
import com.jin.webvideo.util.base.StringUtil;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class BilibiliApi extends AbstractVideoAPI{

    private static final String BASEINFO_URL = "http://api.bilibili.com/pgc/view/web/season?%s_id=%s";
    private static List<String> mediaType = Arrays.asList(new String[]{"", "番剧", "电影", "纪录片", "国创", "电视剧", "综艺"});

    @Override
    public Filmography baseInfo(String docid, String tvid) throws UnavailableServiceException {
        Filmography filmography = baseInfoWithVideoInfoList(docid, tvid, 0, 10000);
        filmography.setVideoInfos(new ArrayList<>());
        return filmography;
    }

    @Override
    public List<FilmographyInfo> videoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        return baseInfoWithVideoInfoList(docid, tvid, pageNo, pageSize).getVideoInfos();
    }

    @Override
    public Filmography baseInfoWithVideoInfoList(String docid, String tvid, Integer pageNo, Integer pageSize) throws UnavailableServiceException {
        try {
            return getBaseInfo(docid, tvid, pageNo, pageSize);
        } catch (IOException e) {
            e.printStackTrace();
            throw new UnavailableServiceException();
        }
    }

    @Override
    String platformName() {
        return PlatformSupport.PLATFORM_BILIBILI;
    }




    private Filmography getBaseInfo(String docid, String cid, Integer pageNo, Integer pageSize) throws IOException {
        if (StringUtil.isNumber(cid)){
            cid = "ep" + cid;
        }
        String idType = "";
        if (cid.startsWith("ep")){
            idType = "ep";
        }
        else if (cid.startsWith("ss")){
            idType = "season";
        }
        String json = ClientUtil.httpGet(String.format(BASEINFO_URL, idType, cid.substring(2)));
        JSONObject obj = JSONObject.parseObject(json);
        if (0 != obj.getInteger("code")){
            throw new IOException("b接口调用失败");
        }
        JSONObject result = obj.getJSONObject("result");
        Filmography filmography = new Filmography();
        filmography.setDesc(result.getString("evaluate"));
        String type = CollectionsUtil.getOrDefault(mediaType, result.getInteger("type"), "");
        filmography.setTag(type);
        if (result.containsKey("new_ep")){
            filmography.setVideoMate( result.getJSONObject("new_ep").getString("desc"));
        }
        filmography.setImg(result.getString("cover"));
        filmography.setId(cid);
        filmography.setName(result.getString("title"));
        filmography.setPlatform(PlatformSupport.PLATFORM_BILIBILI);
        filmography.setPlatformName(PlatformSupport.PLATFORM_NAME_BILIBILI);
        filmography.setDisplayMode(FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK);
        int mode = idType.equals("ep")? FilmographyInfo.DISPLAY_MODE_TITLE_BLOCK: FilmographyInfo.DISPLAY_MODE_INDEX_BLOCK;
        filmography.setVideoInfos(toVideoInfos(result.getJSONArray("episodes"), mode));
        return filmography;
    }

    private List<FilmographyInfo> toVideoInfos(JSONArray array, int mode){
        List<FilmographyInfo> list = new ArrayList<>();
        if (null == array){
            return list;
        }
        for (int i = 0, l = array.size(); i < l; i ++){
            JSONObject obj = array.getJSONObject(i);
            FilmographyInfo info = new FilmographyInfo();
            info.setImg(obj.getString("cover"));
            info.setUrl(obj.getString("link"));
            info.setId(getId(info.getUrl()));
            info.setTitle(String.format("第%d话 %s", i +1, obj.getString("long_title")));
            info.setPlatform(PlatformSupport.PLATFORM_BILIBILI);
            info.setNumber(i+1);
            info.setDisplayMode(mode);
            list.add(info);
        }
        return list;
    }
    private String getId(String url){
        String[] paths = url.split("/");
        String tvid = paths[paths.length-1];
        int index = tvid.indexOf("?");
        if (index != -1){
            tvid = tvid.substring(0, index);
        }
        return tvid;
    }
}
